#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
using namespace std;
using ll = long long;
const int N = 500010;
char c[N];
int n;
int main()
{
    cin >> n >> c;
    ll a = 0, b = 0, ans = 0;
    int pos = 0;
    c[n] = 'H';
    while (c[pos] != 'H') pos++, a++;
    for (int i = pos + 1; i <= n; i++)
    {
        if (c[i] == 'G') b++;
        else
        {
            if (a > 0 && b > 0) ans += a * b + a + b - 2;
            else if (a > 0) ans += a - 1;
            else if (b > 0) ans += b - 1;
            a = b;
            b = 0;
        }
    }
    pos = 0, a = 0, b = 0;
    c[n] = 'G';
    while (c[pos] != 'G') pos++, a++;
    for (int i = pos + 1; i <= n; i++)
    {
        if (c[i] == 'H') b++;
        else
        {
            if (a > 0 && b > 0) ans += a * b + a + b - 2;
            else if (a > 0) ans += a - 1;
            else if (b > 0) ans += b - 1;
            a = b;
            b = 0;
        }
    }
    cout << ans << endl;
    return 0;
}